<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Unit1 extends Page
{
       public $hfForm = null;
       public $hfType = null;
       public $hiddenParts = null;
       public $hiddenWO = null;
       public $hiddenVeh = null;
       function Unit1Create($sender, $params)
       {
       $veh = $_GET['veh'];
       $WO = $_GET['WO'];
       $parts = $_GET['parts'];
       $type = $_GET['type'];
       $form = $_GET['form'];
       $this->hfType->setValue($type);
       $this->hfForm->setValue($form);
       $this->hiddenVeh->setValue($veh);
       $this->hiddenWO->setValue($WO);
       $this->hiddenParts->setValue($parts);

       $query_select = "SELECT quantity,part_num FROM veh_parts_tbl WHERE parts_id='".$this->hiddenParts->getValue()."'";
       $result_select = mysql_query($query_select) or die("SELECT ".mysql_error());
       $row_select = mysql_fetch_array($result_select);

       $stock_id = $row_select['part_num'];
       $qty_delete = $row_select['quantity'];

       $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
       $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
       $row_inv = mysql_fetch_array($result_inv);
       $curr_qty = $row_inv['qty_available'];

       $new_qty = $curr_qty + $qty_delete;
       $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
       mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());

       $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
       $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
       $count_inv_stat = mysql_num_rows($result_select_inv_stat);

       if($count_inv_stat>0)
       {
        $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
        mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
       }

       $result = mysql_query("delete from veh_parts_tbl where parts_id='".$this->hiddenParts->getValue()."'")
       or die(mysql_error());
       redirect('veh_parts_list.php?form='.$this->hfForm->getValue().'&type='.$this->hfType->getValue().'&veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue().'&parts='.$this->hiddenParts->getValue());


       }

}

global $application;

global $Unit1;

//Creates the form
$Unit1=new Unit1($application);

//Read from resource file
$Unit1->loadResource(__FILE__);

//Shows the form
$Unit1->show();

?>